package com.fc.v2.controller.gen;

import com.fc.v2.common.base.BaseController;
import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.common.domain.ResultTable;
import com.fc.v2.model.Examine;
import com.fc.v2.model.auto.*;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.service.ApplyAlterService;
import com.fc.v2.service.ApplyProjectService;
import com.fc.v2.service.FinishProjectService;
import com.fc.v2.shiro.util.ShiroUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller
 * @ClassName: ApplyAlterController
 * @author Archie
 * @date 2021-11-25 12:28:17
 */
@Api(value = "")
@Controller
@RequestMapping("/ApplyAlterDanweiController")
public class ApplyAlterDanweiController extends BaseController{
	
	private String prefix = "gen/applyAlterDanwei";
	
	@Autowired
	private ApplyAlterService applyAlterService;

	@Autowired
	private ApplyProjectService applyProjectService;

	@Autowired
	private FinishProjectService finishProjectService;
	
	
	/**
	 * 页面展示
	 * @param model
	 * @return String
	 * @author Archie
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	@RequiresPermissions("gen:applyAlter:view")
    public String view(ModelMap model)
    {
        return prefix + "/list";
    }
	
	/**
	 * list集合
	 * @param tablepar
	 * @param searchText
	 * @return
	 */
	//@Log(title = "", action = "111")
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/list")
	@RequiresPermissions("gen:applyAlter:list")
	@ResponseBody
	public ResultTable list(Tablepar tablepar, ApplyAlter applyAlter, Model model){
		PageInfo<ApplyAlter> page=applyAlterService.listDanwei(tablepar,applyAlter) ;

		return pageTable(page.getList(),page.getTotal());
	}

	/**
	 * 项目选择跳转
	 */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
	@GetMapping("/select")
	public String select(Model model)
	{
		//排除已变更的项目
		ApplyAlterExample applyAlterExample = new ApplyAlterExample();
		applyAlterExample.createCriteria().andManIdEqualTo(ShiroUtils.getUserId());
		List<ApplyAlter> applyAlterList = new ArrayList<>();
		applyAlterList = applyAlterService.selectByExample(applyAlterExample);
		List<String> ids = new ArrayList<>();
		for (ApplyAlter a: applyAlterList) {
			ids.add(a.getProId());
		}
		//排除结项的项目
		FinishProjectExample finishProjectExample = new FinishProjectExample();
		finishProjectExample.createCriteria().andManIdEqualTo(ShiroUtils.getUserId());
		List<FinishProject> finishProjectList = new ArrayList<>();
		finishProjectList = finishProjectService.selectByExample(finishProjectExample);
		for (FinishProject fp: finishProjectList) {
			ids.add(fp.getApplyId());
		}
		//获取立项项目信息
		ApplyProjectExample example = new ApplyProjectExample();
		example.createCriteria().andManIdEqualTo(ShiroUtils.getUserId()).andProStateEqualTo(Examine.PASSED.code).andIdNotIn(ids);
		List<ApplyProject> applyProjectList = applyProjectService.selectByExample(example);
//		FinishProjectExample example2 = new FinishProjectExample();
//		example2.createCriteria().andManIdEqualTo(ShiroUtils.getUserId()).andProStateEqualTo(Examine.NEED_EXAMINE.code);
		model.addAttribute("applyProjectList",applyProjectList);
		return prefix + "/select";
	}
	
	/**
     * 新增跳转
     */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add/{proId}")
    public String add(ModelMap modelMap,@PathVariable("proId") String proId)
    {
    	modelMap.put("applyProject",applyProjectService.selectByPrimaryKey(proId));
        return prefix + "/add";
    }
	
    /**
     * 新增保存
     * @param 
     * @return
     */
	//@Log(title = "新增", action = "111")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add")
	@RequiresPermissions("gen:applyAlter:add")
	@ResponseBody
	public AjaxResult add(ApplyAlter applyAlter){
		int b=applyAlterService.insertSelective(applyAlter);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除", action = "111")
	@ApiOperation(value = "删除", notes = "删除")
	@DeleteMapping("/remove")
	@RequiresPermissions("gen:applyAlter:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=applyAlterService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	/**
	 * 查看跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "查看跳转", notes = "查看跳转")
	@GetMapping("/check/{id}")
	public String check(@PathVariable("id") String id, ModelMap map)
	{
		map.put("ApplyAlter", applyAlterService.selectByPrimaryKey(id));

		return prefix + "/check";
	}
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
        map.put("ApplyAlter", applyAlterService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "修改", action = "111")
	@ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:applyAlter:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApplyAlter applyAlter)
    {
        return toAjax(applyAlterService.updateByPrimaryKeySelectiveDanwei(applyAlter));
    }
    
    
    /**
	 * 修改状态
	 * @param record
	 * @return
	 */
    @PutMapping("/updateVisible")
	@ResponseBody
    public AjaxResult updateVisible(@RequestBody ApplyAlter applyAlter){
		int i=applyAlterService.updateVisible(applyAlter);
		return toAjax(i);
	}

    
    

	
}
